package com.kc.data.common.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kc.data.common.domain.model.BizGoodsCategory;
import com.kc.data.common.mapper.BizGoodsCategoryMapper;
import com.kc.data.common.service.IBizGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品类别管理
 */
@Service
public class BizGoodsCategoryServiceImpl implements IBizGoodsCategoryService {
    @Autowired
    private BizGoodsCategoryMapper goodsCategoryMapper;

    /**
     * 分类列表
     * @param bizGoodsCategory
     * @return
     */
    @Override
    public List<BizGoodsCategory> getList(BizGoodsCategory bizGoodsCategory) {
        LambdaQueryWrapper<BizGoodsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizGoodsCategory::getIsDeleted,0);
        wrapper.eq(BizGoodsCategory::getStatus,1);
        List<BizGoodsCategory> goodsCategoryList = goodsCategoryMapper.selectList(wrapper);
        return goodsCategoryList;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public BizGoodsCategory getInfo(Integer id) {
        return goodsCategoryMapper.selectById(id);
    }

    /**
     * 商品分类修改
     * @param bizGoodsCategory
     * @return
     */
    @Override
    public Integer edit(BizGoodsCategory bizGoodsCategory) {
        return goodsCategoryMapper.updateById(bizGoodsCategory);
    }

    /**
     * 新增操作
     * @param bizGoodsCategory
     * @return
     */
    @Override
    public Integer add(BizGoodsCategory bizGoodsCategory) {

        bizGoodsCategory.setIsDeleted(0);
        bizGoodsCategory.setStatus(1);
        return goodsCategoryMapper.insert(bizGoodsCategory);
    }

    /**
     * 类别删除
     * @param bizGoodsCategory
     * @return
     */
    @Override
    public Integer delete(BizGoodsCategory bizGoodsCategory) {
        return goodsCategoryMapper.updateById(bizGoodsCategory);
    }
    /**
     * 构建树形类别
     *
     * @param types 类别列表
     * @return 类别列表
     */
    @Override
    public List<BizGoodsCategory> buildTypes(List<BizGoodsCategory> types) {
        // 2. 找出所有一级分类
        //    在映射到每个一级分类 添加它的子分类类
        return types.stream()
                .filter(o -> o.getPid() == 0)
                // 给每个一级分类加子分类
                .peek(o -> o.setChildren(getChildCategoryList(o, types)))
                // 排序
                .sorted(Comparator.comparingInt(BizGoodsCategory::getSort))
                // 收集
                .collect(Collectors.toList());
    }
    // 根据当前分类 找出子类， 并通过递归找出子类的子类
    private List<BizGoodsCategory> getChildCategoryList(BizGoodsCategory currType, List<BizGoodsCategory> types) {
        return types.stream().filter(o -> o.getPid().equals(currType.getId()))
                .peek(o -> o.setChildren(getChildCategoryList(o, types)))
                .sorted(Comparator.comparingInt(BizGoodsCategory::getSort))
                .collect(Collectors.toList());
    }
}
